<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY owl   "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd   "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY vivo  "http://vivoweb.org/ontology/core#" >
<!ENTITY foaf  "http://xmlns.com/foaf/0.1/" >
<!ENTITY for   "http://www.ands.org.au/ontologies/ns/for/2008/" >
<!ENTITY rfcd  "http://www.ands.org.au/ontologies/ns/rfcd/1998/" >
<!ENTITY seo08 "http://www.ands.org.au/ontologies/ns/seo/2008/" >
<!ENTITY seo98 "http://www.ands.org.au/ontologies/ns/seo/1998/" >
<!ENTITY ands  "http://www.ands.org.au/ontologies/ns/0.1/VITRO-ANDS.owl#" >
<!ENTITY rifcs "http://ands.org.au/standards/rif-cs/registryObjects" >
<!ENTITY event "http://purl.org/NET/c4dm/event.owl#" >
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:owl="http://www.w3.org/2002/07/owl#"
	xmlns:vitro="http://vitro.mannlib.cornell.edu/ns/vitro/0.7#"
	xmlns:ands="http://www.ands.org.au/ontologies/ns/0.1/VITRO-ANDS.owl#"
	xmlns:bibo="http://purl.org/ontology/bibo/" xmlns:foaf="http://xmlns.com/foaf/0.1/"
	xmlns:dcterms="http://purl.org/dc/terms/" xmlns:vivo="http://vivoweb.org/ontology/core#"
	xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:event="http://purl.org/NET/c4dm/event.owl#"
	xmlns:rifcs="http://ands.org.au/standards/rif-cs/registryObjects"
	xmlns="http://www.openarchives.org/OAI/2.0/"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<xsl:output method="xml" encoding="UTF-8" indent="yes" />
	<xsl:template match="rdf:RDF">
		<collection>
			<xsl:for-each
				select="rdf:Description[rdf:type/@rdf:resource='&vivo;Course']">
				<xsl:call-template name="researchActivity" />
			</xsl:for-each>
			<xsl:for-each
				select="rdf:Description[rdf:type/@rdf:resource='&event;Event']">
				<xsl:call-template name="researchActivity" />
			</xsl:for-each>
			<xsl:for-each
				select="rdf:Description[rdf:type/@rdf:resource='&vivo;Program']">
				<xsl:call-template name="researchActivity" />
			</xsl:for-each>
			<xsl:for-each
				select="rdf:Description[rdf:type/@rdf:resource='&foaf;Project']">
				<xsl:call-template name="researchActivity" />
			</xsl:for-each>
			<xsl:for-each
				select="rdf:Description[rdf:type/@rdf:resource='&vivo;awardOrDistinction']">
				<xsl:call-template name="researchActivity" />
			</xsl:for-each>

			<!--
				the following code can be simplified if the rdf output contains
				both, the spcific class-type and the super class type
			-->
			<xsl:if test="true()">
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchCatalog']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchCollection']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchDataSet']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchRecordsCollection']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchRegistry']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
				<xsl:for-each
					select="rdf:Description[rdf:type/@rdf:resource='&ands;researchRepository']">
					<xsl:call-template name="researchData" />
				</xsl:for-each>
			</xsl:if>
			<!-- this would be the replacement -->
			<xsl:if test="false()">
				<xsl:apply-templates
					select="rdf:Description[rdf:type/@rdf:resource='&ands;ResearchData']" /> <!-- Gerhard changed case form researchData to ResearchData -->
			</xsl:if>


			<xsl:apply-templates
				select="rdf:Description[rdf:type/@rdf:resource='&foaf;Person']" />
			<xsl:apply-templates
				select="rdf:Description[rdf:type/@rdf:resource='&foaf;Organization']" />


			<xsl:apply-templates
				select="rdf:Description[rdf:type/@rdf:resource='&vivo;Service']" />

		</collection>
	</xsl:template>

	<!-- Activities -->
	<xsl:template name="researchActivity">
		<xsl:variable name="activitytype">
			<xsl:choose>
				<xsl:when test="rdf:type[@rdf:resource='&vivo;Course']">
					<xsl:value-of select="'course'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&event;Event']">
					<xsl:value-of select="'event'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&vivo;Program']">
					<xsl:value-of select="'program'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&foaf;Project']"> <!-- TODO: will be vivo:Project in 1.1 -->
					<xsl:value-of select="'project'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&vivo;awardOrDistinction']">
					<xsl:value-of select="'award'" />
				</xsl:when>
				<xsl:otherwise>
					<!-- TODO: check if this is the correct fallback -->
					<xsl:value-of select="'project'" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<record xmlns="http://www.openarchives.org/OAI/2.0/">
			<xsl:call-template name="oaiheader" />
			<metadata>
				<!-- here we genereate rif metadata -->
				<registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/home/orca/schemata/registryObjects.xsd">
					<registryObject group="{ands:groupDescription}">
						<key>
							<xsl:value-of select="@rdf:about" />
						</key>
						<originatingSource>
							<xsl:value-of select="ands:originatingSource" />
						</originatingSource> <!-- TODO: allow alternatize default value if value is not in rdf -->

						<activity type="{$activitytype}">

							<name type="primary">
								<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
									<xsl:value-of select="rdfs:label" />
									<xsl:value-of select="dcterms:title" />
								</namePart>
							</name>

							<!-- identifier -->
							<xsl:apply-templates select="ands:abn" />
							<xsl:apply-templates select="ands:arc" />
							<xsl:apply-templates select="ands:ark" />
							<xsl:apply-templates select="bibo:doi" />
							<xsl:apply-templates select="bibo:handle" />
							<xsl:apply-templates select="ands:infouri" />
							<xsl:apply-templates select="ands:isil" />
							<!-- TODO: local -->
							<xsl:apply-templates select="ands:nla" />
							<xsl:apply-templates select="ands:purl" />
							<xsl:apply-templates select="bibo:uri" />
	
							<!-- TODO: location-type="coverage" -->
							<xsl:call-template name="physicalAddress" />
							<xsl:call-template name="electronicAddress" />

							<xsl:apply-templates select="ands:describes" />
							<xsl:apply-templates select="ands:isDescribedBy" />
							<xsl:apply-templates select="vivo:hasPart" />
							<xsl:apply-templates select="vivo:isPartOf" />
							<xsl:apply-templates select="dcterms:relation" />
							<xsl:apply-templates select="ands:isLocatedIn" />
							<xsl:apply-templates select="ands:isLocationFor" />
							<xsl:apply-templates select="ands:hasCollector" />
							<xsl:apply-templates select="ands:isManagedBy" />
							<xsl:apply-templates select="ands:supports" />
							<xsl:apply-templates select="ands:isOutputOf" />

							<!-- TODO: not yet done -->
							<xsl:apply-templates select="vivo:hasSubjectArea" />

							<xsl:apply-templates select="vivo:description" />
							<xsl:apply-templates select="bibo:shortDescription" />
							<xsl:apply-templates select="foaf:logo" />
							<xsl:apply-templates select="skos:note" />
							<xsl:apply-templates select="dcterms:rights" />
							<xsl:apply-templates select="dcterms:accessRights" />

							<!-- TODO: not yet done -->
							<!-- relatedinfo -->
						</activity>

					</registryObject>
				</registryObjects>
			</metadata>
		</record>
	</xsl:template>


	<!--  Collection: an aggregation of physical or digital objects;-->
	<xsl:template name="researchData"
		match="rdf:Description[rdf:type/@rdf:resource='&ands;ResearchData']">
		<xsl:variable name="collectiontype">
			<xsl:choose>
				<xsl:when test="rdf:type[@rdf:resource='&ands;researchCatalog']">
					<xsl:value-of select="'catalogueOrIndex'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&ands;researchCollection']">
					<xsl:value-of select="'collection'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&ands;researchDataSet']">
					<xsl:value-of select="'dataset'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&ands;researchRegistry']">
					<xsl:value-of select="'registry'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&ands;researchRepository']">
					<xsl:value-of select="'repository'" />
				</xsl:when>
				<xsl:otherwise>
					<!-- TODO: check if this is the correct fallback -->
					<xsl:value-of select="'dataset'" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<record xmlns="http://www.openarchives.org/OAI/2.0/">
			<xsl:call-template name="oaiheader" />

			<metadata>
				<!-- here we genereate rif metadata -->
				<registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/home/orca/schemata/registryObjects.xsd">
					<registryObject group="{ands:groupDescription}">
						<key>
							<xsl:value-of select="@rdf:about" />
						</key>
						<originatingSource>
							<xsl:value-of select="ands:originatingSource" />
						</originatingSource> <!-- TODO: allow alternatize default value if value is not in rdf -->
						<collection type="{$collectiontype}"
							dateModified="{ands:firstHarvested}">
							<!-- TODO: collection type -->
							<!-- TODO: key fields and identifier should be just hash -->
							
							<name type="primary">
								<namePart  type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
									<xsl:value-of select="rdfs:label" />
									<xsl:value-of select="dcterms:title" />
								</namePart>
							</name>

							<xsl:apply-templates select="ands:abn" />
							<xsl:apply-templates select="ands:arc" />
							<xsl:apply-templates select="ands:ark" />
							<xsl:apply-templates select="bibo:doi" />
							<xsl:apply-templates select="bibo:handle" />
							<xsl:apply-templates select="ands:infouri" />
							<xsl:apply-templates select="ands:isil" />

							<!-- TODO: local -->
							<xsl:apply-templates select="ands:nla" />
							<xsl:apply-templates select="ands:purl" />
							<xsl:apply-templates select="bibo:uri" />

							<xsl:apply-templates select="ands:custodianDepartment" />

							<!-- TODO: location-type="coverage" -->
							<xsl:call-template name="physicalAddress" />
							<xsl:call-template name="electronicAddress" />
							<xsl:apply-templates select="ands:hasParticipant" />
							<xsl:apply-templates select="vivo:fundedBy" />
							<xsl:apply-templates select="ands:isManagedBy" />
							<xsl:apply-templates select="ands:isOwnedBy" />
							<xsl:apply-templates select="ands:hasOutput" />
							<xsl:apply-templates select="dcterms:relation" />
							<xsl:apply-templates select="vivo:hasPart" />
							<xsl:apply-templates select="vivo:isPartOf" />

							<!-- TODO: not yet done -->
							<xsl:apply-templates select="vivo:hasSubjectArea" />

							<xsl:apply-templates select="vivo:description" />
							<xsl:apply-templates select="vitro:description" />
							<xsl:apply-templates select="bibo:shortDescription" />
							<xsl:apply-templates select="foaf:logo" />
							<xsl:apply-templates select="skos:note" />
							<xsl:apply-templates select="dcterms:rights" />
							<xsl:apply-templates select="dcterms:accessRights" />

							<!-- TODO: not yet done -->
							<!-- relatedinfo -->

						</collection>
					</registryObject>
				</registryObjects>
			</metadata>
		</record>
	</xsl:template>


	<!--  Party: a person or group; -->
	<xsl:template match="rdf:Description[rdf:type/@rdf:resource='&foaf;Person']">
		<xsl:call-template name="researchParty" />
	</xsl:template>

	<xsl:template
		match="rdf:Description[rdf:type/@rdf:resource='&foaf;Organization']">
		<xsl:call-template name="researchParty" />
	</xsl:template>

	<xsl:template name="researchParty">
		<xsl:variable name="partytype">
			<xsl:choose>
				<xsl:when test="rdf:type[@rdf:resource='&foaf;Person']">
					<xsl:value-of select="'person'" />
				</xsl:when>
				<xsl:when test="rdf:type[@rdf:resource='&foaf;Organization']">
					<xsl:value-of select="'group'" />
				</xsl:when>
				<xsl:otherwise>
					<!-- TODO: check if this is the correct fallback -->
					<xsl:value-of select="'group'" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<record xmlns="http://www.openarchives.org/OAI/2.0/">
			<xsl:call-template name="oaiheader" />
			<metadata>
				<!-- here we genereate rif metadata -->
			<registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/home/orca/schemata/registryObjects.xsd">
						<registryObject group="{ands:groupDescription}">
						<key>
							<xsl:value-of select="@rdf:about" />
						</key>
						<originatingSource>
							<xsl:value-of select="ands:originatingSource" />
						</originatingSource> <!-- TODO: allow alternatize default value if value is not in rdf -->

						<party type="{$partytype}">

							<name type="primary">
								<xsl:call-template name="partyname" />
							</name>

							<!-- identifier -->
							<xsl:apply-templates select="ands:abn" />
							<xsl:apply-templates select="ands:arc" />
							<xsl:apply-templates select="ands:ark" />
							<xsl:apply-templates select="bibo:doi" />
							<xsl:apply-templates select="bibo:handle" />
							<xsl:apply-templates select="ands:infouri" />
							<xsl:apply-templates select="ands:isil" />
							<!-- TODO: local -->
							<xsl:apply-templates select="ands:nla" />
							<xsl:apply-templates select="ands:purl" />
							<xsl:apply-templates select="bibo:uri" />

							
							<!-- TODO: location-type="coverage" -->
							<xsl:call-template name="physicalAddress" />
							<xsl:call-template name="electronicAddress" />

							<xsl:apply-templates select="dcterms:relation" />
							<xsl:apply-templates select="vivo:fundedBy" />
							<xsl:apply-templates select="vivo:funds" />
							<xsl:apply-templates select="ands:isOwnerOf" />
							<xsl:apply-templates select="ands:isParticipantIn" />
							<xsl:apply-templates select="ands:isCollectorOf" />
							<xsl:apply-templates select="vivo:hasPart" />
							<xsl:apply-templates select="vivo:isPartOf" />
							<xsl:apply-templates select="vivo:hasMember" />
							<xsl:apply-templates select="vivo:memberOf" />
							<xsl:apply-templates select="ands:isManagedBy" />
							<xsl:apply-templates select="ands:isManagerOf" />


							<!-- TODO: not yet done -->
							<xsl:apply-templates select="vivo:hasSubjectArea" />

							<xsl:apply-templates select="vivo:description" />
							<xsl:apply-templates select="bibo:shortDescription" />
							<xsl:apply-templates select="foaf:logo" />
							<xsl:apply-templates select="skos:note" />
							<xsl:apply-templates select="dcterms:rights" />
							<xsl:apply-templates select="dcterms:accessRights" />

							<!-- TODO: not yet done -->
							<!-- relatedinfo -->
						</party>

					</registryObject>
				</registryObjects>
			</metadata>
		</record>
	</xsl:template>


	<xsl:template match="rdf:Description[rdf:type/@rdf:resource='&vivo;Service']">
		<record xmlns="http://www.openarchives.org/OAI/2.0/">
			<xsl:call-template name="oaiheader" />
			<metadata>
			<registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/home/orca/schemata/registryObjects.xsd">
						<registryObject group="{ands:groupDescription}">
						<key>
							<xsl:value-of select="@rdf:about" />
						</key>
						<originatingSource>
							<xsl:value-of select="ands:originatingSource" />
						</originatingSource>
						<service type="{vivo:type}">
							<xsl:apply-templates select="ands:abn" />
							<xsl:apply-templates select="ands:arc" />
							<xsl:apply-templates select="ands:ark" />
							<xsl:apply-templates select="bibo:doi" />
							<xsl:apply-templates select="bibo:handle" />
							<xsl:apply-templates select="ands:infouri" />
							<xsl:apply-templates select="ands:isil" />
							<xsl:apply-templates select="ands:nla" />
							<xsl:apply-templates select="ands:purl" />
							<xsl:apply-templates select="bibo:uri" />
							<name type="primary">
								<namePart  type="title"  xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
									<xsl:value-of select="foaf:name" />
								</namePart>
							</name>
							<xsl:call-template name="physicalAddress" />
							<xsl:call-template name="electronicAddress" />
							<xsl:apply-templates select="dcterms:relation" />
							<xsl:apply-templates select="ands:isManagedBy" />
							<xsl:apply-templates select="ands:isOwnedBy" />
							<xsl:apply-templates select="ands:isSupportedBy" />
							<xsl:apply-templates select="vivo:hasPart" />
							<xsl:apply-templates select="vivo:isPartOf" />
							<xsl:apply-templates select="vivo:hasSubjectArea" />
							<xsl:apply-templates select="vivo:description" />
							<xsl:apply-templates select="bibo:shortDescription" />
							<xsl:apply-templates select="foaf:logo" />
							<xsl:apply-templates select="skos:note" />
							<xsl:apply-templates select="dcterms:rights" />
							<xsl:apply-templates select="dcterms:accessRights" />
						</service>
					</registryObject>
				</registryObjects>
			</metadata>
		</record>
	</xsl:template>
	<xsl:template name="partyname">
		<xsl:apply-templates select="foaf:name" />
		<xsl:apply-templates select="foaf:title" />
		<xsl:apply-templates select="foaf:givenName" />
		<xsl:apply-templates select="foaf:familyName" />
		<xsl:apply-templates select="bibo:suffixName" />
	</xsl:template>
	<xsl:template match="foaf:name">
		<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</namePart>
	</xsl:template>
	<xsl:template match="foaf:title">
		<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</namePart>
	</xsl:template>
	<xsl:template match="foaf:givenName">
		<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</namePart>
	</xsl:template>
	<xsl:template match="foaf:familyName">
		<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</namePart>
	</xsl:template>
	<xsl:template match="bibo:suffixName">
		<namePart type="title" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</namePart>
	</xsl:template>
	<xsl:template match="ands:abn">
		<identifier type="abn">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="ands:arc">
		<identifier type="arc">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="ands:ark">
		<identifier type="ark">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="bibo:doi">
		<identifier type="doi">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="bibo:handle">
		<identifier type="handle">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="ands:infouri">
		<identifier type="infouri">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="ands:isil">
		<identifier type="isil">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="ands:purl">
		<identifier type="purl">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template match="bibo:uri">
		<identifier type="uri">
			<xsl:value-of select="." />
		</identifier>
	</xsl:template>
	<xsl:template name="physicalAddress">
		<xsl:if test="ands:streetAddress or vivo:mailingAddress">
			<location>
				<address>
					<xsl:apply-templates select="ands:streetAddress" />
					<xsl:apply-templates select="vivo:mailingAddress" />
				</address>
			</location>
		</xsl:if>
	</xsl:template>
	<xsl:template match="ands:streetAddress">
		<xsl:variable name="resuri" select="@rdf:resource" />
		<xsl:variable name="address"
			select="//rdf:Description[@rdf:about=$resuri]" />
		<xsl:if test="$address">
			<physical type="streetAddress">
				<xsl:apply-templates select="$address/vivo:addressDepartment" />
				<xsl:apply-templates select="$address/vivo:addressStreet" />
				<xsl:apply-templates select="$address/vivo:addressCity" />
				<xsl:apply-templates select="$address/vivo:addressState" />
				<xsl:apply-templates select="$address/vivo:addressCountry" />
				<xsl:apply-templates select="$address/vivo:addressPostalCode" />
			</physical>
		</xsl:if>
	</xsl:template>
	<xsl:template match="vivo:mailingAddress">
		<xsl:variable name="resuri" select="@rdf:resource" />
		<xsl:variable name="address"
			select="//rdf:Description[@rdf:about=$resuri]" />
		<xsl:if test="$address">
			<physical type="postalAddress">
				<xsl:apply-templates select="$address/vivo:addressDepartment" />
				<xsl:apply-templates select="$address/vivo:addressStreet" />
				<xsl:apply-templates select="$address/vivo:addressCity" />
				<xsl:apply-templates select="$address/vivo:addressState" />
				<xsl:apply-templates select="$address/vivo:addressCountry" />
				<xsl:apply-templates select="$address/vivo:addressPostalCode" />
			</physical>
		</xsl:if>
	</xsl:template>
	<xsl:template match="vivo:addressDepartment">
		<addressPart type="locationDescriptor">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template match="vivo:addressStreet">
		<addressPart type="streetName">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template match="vivo:addressCity">
		<addressPart type="suburbOrPlaceOrLocality">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template match="vivo:addressState">
		<addressPart type="stateOrTerritory">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template match="vivo:addressCountry">
		<addressPart type="country">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template match="vivo:addressPostalCode">
		<addressPart type="postCode">
			<xsl:value-of select="." />
		</addressPart>
	</xsl:template>
	<xsl:template name="electronicAddress">
		<xsl:if
			test="vivo:workEmail or vivo:workFax or vivo:webpage or vivo:workPhone or ands:wsdl">
			<location>
				<address>
					<xsl:apply-templates select="vivo:workEmail" />
					<xsl:apply-templates select="vivo:workFax" />
					<xsl:apply-templates select="vivo:workPhone" />
					<xsl:apply-templates select="vivo:webpage" />
					<!-- <xsl:apply-templates select="ands:wsdl" /> -->
				</address>
			</location>
		</xsl:if>
	</xsl:template>
	<xsl:template match="vivo:workEmail">
		<electronic type="email">
			<value>
				<xsl:value-of select="." />
			</value>
		</electronic>
	</xsl:template>
	<xsl:template match="vivo:workFax">
		<xsl:if test="./text()"> <!-- don't generate empty tags TODO: do this everywhere? -->
			<electronic type="fax">
				<value>
					<xsl:value-of select="." />
				</value>
			</electronic>
		</xsl:if>
	</xsl:template>
	<xsl:template match="vivo:workPhone">
		<electronic type="voice">
			<value>
				<xsl:value-of select="." />
			</value>
		</electronic>
	</xsl:template>
	<xsl:template match="vivo:webpage">
		<xsl:variable name="resuri" select="@rdf:resource" />
		<xsl:variable name="linkURI"
			select="//rdf:Description[@rdf:about=$resuri]/vivo:linkURI" />
		<xsl:if test="$linkURI">
			<electronic type="url">
				<value>
					<xsl:value-of select="$linkURI" />
				</value>
			</electronic>
		</xsl:if>
	</xsl:template>
	<xsl:template match="vivo:description">
		<description type="full" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
		<xsl:template match="vitro:description">
		<description type="full" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
	<xsl:template match="bibo:shortDescription">
		<description type="brief" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
	<xsl:template match="foaf:logo">
		<description type="logo" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="@about" />
		</description>
	</xsl:template>
	<xsl:template match="skos:note">
		<description type="note" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
	<xsl:template match="dcterms:rights">
		<description type="rights" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
	<xsl:template match="dcterms:accessRights">
		<description type="accessRights" xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<xsl:value-of select="." />
		</description>
	</xsl:template>
	<xsl:template match="vivo:hasSubjectArea">
		<xsl:choose>
			<xsl:when test="starts-with(@rdf:resource, '&seo98;')">
				<subject type="anzsrc-seo">
					<xsl:value-of select="substring-after(@rdf:resource, '&seo98;')" />
				</subject>
			</xsl:when>
			<xsl:when test="starts-with(@rdf:resource, '&seo08;')">
				<subject type="anzsrc-seo">
					<xsl:value-of select="substring-after(@rdf:resource, '&seo08;')" />
				</subject>
			</xsl:when>
			<xsl:when test="starts-with(@rdf:resource, '&rfcd;')">
				<subject type="anzsrc-rfcd">
					<xsl:value-of select="substring-after(@rdf:resource, '&rfcd;')" />
				</subject>
			</xsl:when>
			<xsl:when test="starts-with(@rdf:resource, '&for;')">
				<subject type="anzsrc-for">
					<xsl:value-of select="substring-after(@rdf:resource, '&for;')" />
				</subject>
			</xsl:when>
			<xsl:otherwise>
				<subject type="local">
					<xsl:value-of select="." />
				</subject>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="vivo:hasMember">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasMember">
				<description>hasMember</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="vivo:memberOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isMemberOf">
				<description>memberOf</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
<xsl:template match="ands:describes">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="describes">
				<description>describes</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isDescribedBy">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isDescribedBy">
				<description>isDescribedBy</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="vivo:hasPart">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasPart">
				<description>hasPart</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="vivo:isPartOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isPartOf">
				<description>isPartOf</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="dcterms:relation">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasAssociatonWith">
				<description>hasAssociatonWith</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isLocatedIn">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isLocatedIn">
				<description>isLocatedIn</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isLocationFor">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isLocationFor">
				<description>isLocationFor</description>
				</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:hasCollector">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasCollector">
				<description>hasCollector</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isCollectorOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isCollectorOf">
				<description>isCollectorOf</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:custodianDepartment">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isManagedBy">
				<description>memberOf</description>
			</relation>
		</relatedObject>
	</xsl:template>
<xsl:template match="ands:isManagedBy">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isManagedBy">
				<description>isManagedBy</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isManagerOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isManagerOf">
				<description>isManagerOf</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:supports">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="supports">
				<description>supports</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isSupportedBy">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isSupportedBy">
				<description>isSupportedBy</description>
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isOutputOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isOutputOf">
				<description>isOutputOf</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:hasOutput">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasOutput">
				<description>hasOutput</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isParticipantIn">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isParticipantIn">
				<description>isParticipantIn</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:hasParticipant">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="hasParticipant">
				<description>hasParticipant</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isOwnerOf">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isOwnerOf">
				<description>isOwnerOf</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="ands:isOwnedBy">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isOwnedBy">
				<description>isOwnedBy</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="vivo:fundedBy">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isFundedBy">
				<description>isFundedBy</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template match="vivo:funds">
		<relatedObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">
			<key>
				<xsl:value-of select="@rdf:resource" />
			</key>
			<relation type="isFunderOf">
				<description>isFunderOf</description>
				<!-- <url></url> -->
			</relation>
		</relatedObject>
	</xsl:template>
	<xsl:template name="oaiidentifier">
		<xsl:param name="uri" />
		<xsl:variable name="domain">
			<!-- skip http:// and use all values until first : or / -->
			<xsl:value-of
				select="substring-before(substring-before(substring-after($uri, 'http://'), '/'), ':')" />
		</xsl:variable>
		<xsl:variable name="pathid">
			<!-- skip http:// and use all after first / -->
			<xsl:value-of
				select="substring-after(substring-after($uri, 'http://'), '/')" />
		</xsl:variable>
		<xsl:value-of select="concat('', $domain, ':', $pathid)" />
	</xsl:template>

	<!-- generate oai record header -->
	<xsl:template name="oaiheader">
		<xsl:variable name="oaiid">
			<xsl:call-template name="oaiidentifier">
				<xsl:with-param name="uri" select="@rdf:about" />
			</xsl:call-template>
		</xsl:variable>
		<database:recordid xmlns:database="http://www.oclc.org/pears/">
			<xsl:value-of select="$oaiid" />
		</database:recordid> <!-- oaicat XML File storage needs this -->
		<header> <!-- status="deleted"> -->
			<identifier>
				<xsl:value-of select="$oaiid" />
			</identifier>
			<datestamp>
				<xsl:for-each select="ands:firstHarvested">
					<xsl:sort  select="." data-type="text" order="descending"/>
					<xsl:if test="position()=1">
						<xsl:value-of select="."/>
					</xsl:if>
				</xsl:for-each>
			</datestamp>
		</header>
	</xsl:template>
	<xsl:template match="text()" />
</xsl:stylesheet>
